from rest_framework import serializers
from course import models
from course.models import Teacher


#被嵌套的分类序列化
class CategorySer(serializers.ModelSerializer):
    class Meta:
        model=models.Category
        fields=["id","cname"]

#分类的序列化
class CategorySerializer(serializers.ModelSerializer):
    #嵌套序列化  children这个字段是在定义模型的时候  通过related_name定义的 如果不定义使用 category_set
    children=CategorySer(many=True)
    class Meta:
        model=models.Category
        fields="__all__"

class TeacherSer(serializers.ModelSerializer):
    class Meta:
        model=models.Teacher
        fields="__all__"

class CourseListSer(serializers.ModelSerializer):
    teacher=TeacherSer()
    class Meta:
        model = models.Course
        fields = "__all__"


#设置索引的序列化器
from course import search_indexes #索引的模型文件
from drf_haystack.serializers import HaystackSerializer #haystack的序列化器
class CourseIndexSerializer(HaystackSerializer):
    class Meta:
        index_classes=[search_indexes.CourseIndex] #指定索引类
        fields=["text","id","coursename","price","cover"] #指定索引字段